package com.frame.service.base.biz.convert.bak.pic;

import com.frame.convert.core.AbstractConverter;
import com.frame.convert.core.ConvertContext;
import com.frame.convert.core.ConvertListContext;
import com.frame.service.base.biz.service.sys.upload.support.UploadSupport;
import com.frame.service.base.biz.service.sys.upload.vo.UploadSelectResVO;
import com.frame.service.base.utils.ApplicationContextUtil;
import org.apache.commons.lang3.StringUtils;

import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.function.Function;
import java.util.stream.Collectors;

/*
 * @author zhangyan
 * @date 2021-04-26 17:37:07
 **/
public class PicListConverter extends AbstractConverter<PicList,String, List<UploadSelectResVO>> {
    @Override
    public List<UploadSelectResVO> convert(ConvertContext<PicList, String> context) {
        return doConvert(context,null);
    }

    @Override
    public Function<ConvertContext<PicList, String>, List<UploadSelectResVO>> convertList(ConvertListContext<PicList, String> convertListContext) {
        List<String> sourceValueList = convertListContext.getSourceValueList();
        PicList annotation = convertListContext.getConverterAnnotation();
        boolean useDefault = annotation.useDefault();
        UploadSupport uploadSupport = getUploadSupport();
        List<UploadSelectResVO> defaultList = Collections.singletonList(uploadSupport.getDefaultPic());
        Function<ConvertContext<PicList,String>,List<UploadSelectResVO>> defaultPicFun= context->defaultList;
        if (sourceValueList.isEmpty()) return useDefault?defaultPicFun:null;
        String picIds = sourceValueList.stream().filter(StringUtils::isNotBlank).collect(Collectors.joining(","));
        List<UploadSelectResVO> uploadList = uploadSupport.getUploadList(picIds);
        if (uploadList==null||uploadList.isEmpty()) return useDefault?defaultPicFun:null;
        return context-> doConvert(context,uploadList);
    }

    private List<UploadSelectResVO> doConvert(ConvertContext<PicList,String> context, List<UploadSelectResVO> uploadSourceList) {
        String picIds = context.getSourceValue();
        PicList annotation = context.getConverterAnnotation();
        boolean useDefault = annotation.useDefault();
        UploadSupport uploadSupport = getUploadSupport();
        List<UploadSelectResVO> defaultList = Collections.singletonList(uploadSupport.getDefaultPic());
        if (StringUtils.isBlank(picIds)) return useDefault? defaultList :null;
        List<UploadSelectResVO> uploadList;
        if (uploadSourceList==null){
            uploadList = uploadSupport.getUploadList(picIds);
        }else{
            uploadList=uploadSupport.extractAndOrderByIds(Arrays.asList(picIds.split(",")),uploadSourceList);
        }
        if (uploadList.isEmpty()) return useDefault?defaultList:null;
        return uploadList;
    }
    private UploadSupport getUploadSupport(){
        return ApplicationContextUtil.getBean(UploadSupport.class);
    }

}
